const url = require('url');
function Router() {
    this.stack = [] // 路由表 
}
Router.prototype.get = function (path, handler) {
    this.stack.push({
        path,
        handler,
        method: 'get'
    })
}
Router.prototype.handle = function (req, res, out) {
    const { pathname: requestPathname } = url.parse(req.url, true);
    const requestMethod = req.method.toLowerCase();

    for (let i = 0; i < this.stack.length; i++) {
        let { path, handler, method } = this.stack[i];
        // 请求的路径和方法都匹配 说明命中了路由
        if (path === requestPathname && method === requestMethod) {
            return handler(req, res)
        }
    }
    return out()
}
module.exports = Router;